package com.Sdebruijn.curriculumvitae.asynctask;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Sdebruijn.curriculumvitae.R;
import com.Sdebruijn.curriculumvitae.db.DatabaseManager;
import com.Sdebruijn.curriculumvitae.fragments.PersonFragment;
import com.Sdebruijn.curriculumvitae.models.Applicant;
import com.Sdebruijn.curriculumvitae.util.ViewUtils;
import com.Sdebruijn.curriculumvitae.view.ProgressWheel;
import com.Sdebruijn.curriculumvitae.view.RemoteImageView;

/**
 * Loads the data of a Person in a separate background thread with
 * {@link AsyncTask}
 * 
 * @author Stefan de Bruijn
 */
// TODO Eventually replace this with a service - per example with RoboSpice
public class LoadPersonDataAsyncTask extends AsyncTask<Void, Void, Applicant> {

	/*
	 * Data fields from the fragment, in order according to the view
	 */
	private LinearLayout ll_person_data;

	private RemoteImageView picture;
	private TextView name;
	private TextView title;
	private TextView company;
	private TextView email;
	private TextView nationality;
	private TextView dob;
	private TextView mobile;

	private TextView tv_linkedin;
	private TextView tv_google_plus;

	private ProgressWheel pg;

	public LoadPersonDataAsyncTask(View view, Context context) {

		ll_person_data = (LinearLayout) view.findViewById(R.id.ll_person_data);

		picture = (RemoteImageView) view.findViewById(R.id.riv_picture);
		name = (TextView) view.findViewById(R.id.tv_name);
		title = (TextView) view.findViewById(R.id.tv_title);
		company = (TextView) view.findViewById(R.id.tv_company);
		email = (TextView) view.findViewById(R.id.tv_email);
		nationality = (TextView) view.findViewById(R.id.tv_nationality);
		dob = (TextView) view.findViewById(R.id.tv_dob);
		mobile = (TextView) view.findViewById(R.id.tv_mobile);
		tv_linkedin = (TextView) view.findViewById(R.id.tv_linkedin);
		tv_google_plus = (TextView) view.findViewById(R.id.tv_google_plus);

		pg = (ProgressWheel) view.findViewById(R.id.pw_spinner);

	}

	@Override
	protected void onPreExecute() {
		// Show the progress wheel and make it spin
		ViewUtils.switchVisibleState(ll_person_data, ViewUtils.State.INVISIBLE, pg,
				ViewUtils.State.VISIBLE);
		pg.spin();
	}

	@Override
	protected Applicant doInBackground(Void... params) {
		return (Applicant) DatabaseManager.getInstance().getApplicant();
	}

	@Override
	protected void onPostExecute(Applicant applicant) {
		picture.setRemotePath(applicant.picture);
		picture.loadImage(true);
		name.setText(applicant.name);
		title.setText(applicant.current_title);
		company.setText(applicant.current_institution);
		email.setText(applicant.email);
		nationality.setText(applicant.nationality);
		dob.setText(applicant.date_of_birth);
		mobile.setText(applicant.mobile);
		tv_linkedin.setText(applicant.online_person.linkedin_url);
		tv_google_plus.setText(applicant.online_person.google_plus_url);

		ViewUtils.switchVisibleState(ll_person_data, ViewUtils.State.VISIBLE, pg,
				ViewUtils.State.INVISIBLE);
		
		pg.stopSpinning();
		PersonFragment.mApplicant = applicant;
	}
}
